import { bg_image } from "./indoorConfig.js";
import {tempLicenseRegister,tempLicenseEnter,} from "../api/myCar/index.js"
import {getUserInfo,portGetInfo} from "../api/home/index.js"
import {getExtraPortInfo,} from "../api/temporaryEntryce/index.js"
import Vue from 'vue'
export default {
	data() {
		return {
			isShowPage: false,
			bg_image,
			portId: '',
			parkingInfo: { enterTime: '', parkName: '', plateNumber: '' }
		}
	},
	async onLoad(options) {
		getUserInfo()
		this.portId = options.portId ?? ''
		if (options.portId) {
			// 获取车场信息
			let portInfoRes = await getExtraPortInfo({
				portId: this.portId
			})
			if(!portInfoRes) return
			let data = portInfoRes.data || {}
			if (!data.isEnterTempLicense) {
				uni.redirectTo({
					url: `/pages/indoor/noLicensePlate?portId=${this.portId}&pageMark=tempLicense&parkName=${data.parkName}&portName=${data.portName}`
				})
				return
			}
			this.isShowPage = true
		}
	},
	methods: {
		/**
		 * 监听车牌输入完成
		 */
		async handleSuccess(plateInfo) {
			if (!this.portId) {
				uniToast('车场不存在无法绑定车牌', 'none')
				return 
			}
			if (!plateInfo.plateNumber || plateInfo.plateNumber.length < 7) {
				uniToast('请检查车牌号是否正确', 'none')
				return
			}
			const param = { portId: this.portId, plateNumber: plateInfo.plateNumber }
			let res = await tempLicenseRegister(param)
			let {plateNumber, enterTime, parkName} = res.data || {}
			this.parkingInfo.plateNumber = res.data.plateNumber
			this.parkingInfo.enterTime = res.data.enterTime
			this.parkingInfo.parkName = res.data.parkName
		},
		/**
		 * 点击确认入场
		 */
		async handleConfirmEntry() {
			const param = { portId: this.portId }
			let res = await tempLicenseEnter(param)
			if(!res) return
			uni.redirectTo({
				url: `/pages/indoor/home`
			})
		}
	}
}